//
//  LoginViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/8.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)buttonPress:(UIButton *)sender{
    
    if ([[AFNetworkReachabilityManager sharedManager] isReachable]) {

        //[self performSegueWithIdentifier:@"Login_Segue" sender:nil];
        
        //登入按鈕 檢查 帳號密碼欄位長度 和 格式
        if (self.TF_account.text.length>0 && self.TF_password.text>0 && [ValidateText validatePhone:self.TF_account.text]  && [ValidateText validatePassword:self.TF_password.text])
        {
            
            //使用 userDefaults 去紀錄
            [[NSUserDefaults standardUserDefaults]setValue:self.TF_account.text forKey:@"account"];
            
            
            [[NSUserDefaults standardUserDefaults]setValue:self.TF_password.text forKey:@"password"];
            
            //這邊註冊了登入後的 delegate
            [XMPPSharedInstance sharedInstance].stateDelegate = self;

            //執行登入
            [[XMPPSharedInstance sharedInstance] loginXMPP];
        }else
        {
            [WinkAlertView _showAlertViewWithTitle:@"注意" content:@"帳號密碼格式不正確" cancelButtonTitle:@"確認" showTime:3.0f];

        }
    }else
    {
        [WinkAlertView _showAlertViewWithTitle:@"注意" content:@"請檢查您的網路" cancelButtonTitle:@"確認" showTime:3.0f];

    }
}
-(IBAction)toRegist:(UIButton *)sender{

    //防止頁面無限迴圈重複切換
    [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(UIViewController *controller, NSUInteger idx, BOOL *stop) {
        if ([controller isKindOfClass:[RegistViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
            *stop = YES;
            
        }else if(idx == self.navigationController.viewControllers.count-1)
        {
            
            //跳到登入頁面
            RegistViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"RegistViewController"];
            [self.navigationController pushViewController:loginVC animated:YES];
        }
        
    }];
}
#pragma mark connectStateDelegate 連線的delegate
-(void)connectState:(int)state
{
    //因為這邊delegate 是在背景執行時觸發 但因為我們有做切換畫面 所以要回到主線程 不然會出問題
    dispatch_async(dispatch_get_main_queue(), ^{
        
        
        switch (state) {
                
            case 0:
                NSLog(@"連線中斷");
                
                break;
            case 1:
                NSLog(@"進行連線");
                
                break;
            case 2:
                NSLog(@"連線成功");
                
                //返回首頁
                [self.navigationController popToRootViewControllerAnimated:YES];
                
                
                break;
            default:
                
                break;
        }
    });
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
